Ext.onReady(function() {
	// it is generally best to disable state management as dynamically-generated ids
	// can change across page loads, leading to unpredictable results.  The developer
	// should ensure that stable state ids are set for stateful components in real apps.
	Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
	
	var moduelsArray;
	//取出模块
	Ext.Ajax.request({
		url: window.__base+'/admin/module/load',
		waitMsg: '正在载入，请稍候。。。',
	   	success: function(response, opts) {
			var moduelsJson = Ext.decode(response.responseText);
			moduelsArray = _$_getAccorPanel(moduelsJson);
			Ext.getCmp('_$_west-panel').add(moduelsArray);
			Ext.getCmp('_$_west-panel').doLayout();
	   	}
	});
	
	var viewport = new Ext.Viewport(
			{
				layout: 'border',
				items: [
						// create instance immediately
						new Ext.BoxComponent(
								{
									region : 'north',
									height : 60,
									contentEl:'north'
								}),
						{
							// lazily created panel (xtype:'panel' is default)
							region : 'south',
							contentEl : 'south',
							split : true,
							height : 60,
							minSize : 100,
							maxSize : 200,
							collapsible : true,
							collapsed:true,
							title : '系统提示',
							margins : '0 0 0 0'
						},
						{
							region : 'east',
							title : '系统动态',
							collapsible : true,
							collapsed:true,
							split : true,
							width : 225,
							minSize : 175,
							maxSize : 400,
							margins : '0 5 0 0',
							layout : 'fit',
							items :
							new Ext.TabPanel(
									{
										border : false,
										activeTab: 1,
										tabPosition : 'bottom',
										items : [
												{
													html : '<p>暂时没有什么可告诉你的.</p>',
													title : '系统公告',
													autoScroll : true
												},
												new Ext.grid.PropertyGrid(
														{
															title : '用户信息',
															closable : true,
															source : {
																"工号" : window._login_usercode,
																"姓名" : window._login_userName,
																"公司代码" : window._login_companyCode,
																"公司名称" : window._login_companyName
															}
														}) ]
									})
						}, {
							region : 'west',
							id : '_$_west-panel',
							title : '功能模块',
							split : true,
							width : 200,
							minSize : 175,
							maxSize : 400,
							collapsible : true,
							margins : '0 0 0 5',
							layout : {
								type : 'accordion',
								animate : true
							}
						},
						new Ext.TabPanel( {
							id:'_$_center_panel',
							region : 'center',
							deferredRender : false,
							activeTab : 0,
							enableTabScroll:true,
							defaults: {
			                    autoScroll: true
			                },
							items : [ {
								html : 'welcome to PICC!',
								title : '系统首页',
								autoScroll : true
							} ],
							plugins: new Ext.ux.TabCloseMenu()
						}) ]
			});
});

function _$_getAccorPanel(array) {
	var panleArray = [];
	Ext.each(array,function(item,index,allItems) {
		if (item.parentId == 0) {
			var panelTree = new Ext.tree.TreePanel({
				xtype:'treepanel',
				animate:true,//以动画形式伸展,收缩子节点  
			    header:false,////是否显示标题
			    border:false,
			    collapsible:true,  
			    rootVisible:false,//是否显示根节点  
			    autoScroll:true,  
			    autoHeight:true,  
			    split: true,  
			    padding:'5 0 0 0',
			    //useArrows:true,
			    width:'100%',  
			    //lines:false,//节点之间连接的横竖线
			    loader: new Ext.tree.TreeLoader({ 
			    	dataUrl:AJAX_ROOT_URL+'/admin/module/loadModuleTree/',
			        preloadChildren: true,//若为true，则loader在节点第一次访问时加载"children"的属性  
			        clearOnLoad: false,
			        listeners:{
			    		'beforeload':function(loader,node) {
			         		loader.dataUrl = AJAX_ROOT_URL+'/admin/module/loadModuleTree/' + node.attributes.id;
				        }
			        }
			    }),  
			    root:new Ext.tree.AsyncTreeNode({  
			    	id:item.id,
			        text:item.name,//节点名称  
			        expanded:true,//展开  
			        leaf:false
				}),
				listeners:{
					click:_$_showCenter
				}
			});
			var panelObj = {title: '<h1 align="center">'+item.name+'</h1>', border: false, items:[panelTree]};
			panleArray.push(panelObj);
//			new Ext.tree.TreeSorter(panelTree, {
//				dir:'asc',
//				folderSort:true,
//				sortType: function(node) {
//					console.dir(node);
//			        // sort by a custom, typed attribute:
//					//alert(node.attributes.orderNumber);
//			        return parseInt(node.id, 10);
//			    }
//
//			}); 
		}
	})
	return panleArray;
}

function _$_showCenter(n,e) {
	//n.isLeaf()是节点本身的函数
	if (n.isLeaf()) {
		var tabPanel = Ext.getCmp('_$_center_panel');
		//获取页面，如果原来已经创建有则不需要再创建，激活即可
		var page = tabPanel.getItem('_$_tab_'+n.id);
		if (page == undefined) {
			//eval('var _$_m'+n.id+' = new '+n.attributes.openView+'({id:'+n.id+',title:\''+n.text+'\'});');
			//page = tabPanel.add(eval('_$_m'+n.id));
			//如果对centerPanel进行遮罩,则可以出现阴影mainTabs
     		tabPanel.getEl().mask('正在加载页面,请稍候...', 'x-mask-loading');
			page = tabPanel.add({
				title:'<font size="2">'+n.attributes.name+'</font>',    
               	id:'_$_tab_'+n.id,    
               	//iconCls: 'tabs',                      
               	margins:'0 0 0 0',    
               	autoHeight:true,    
               	autoScroll:true,    
               	autoWidth:true,    
               	closable:true,    
              	frame:true,    
              	html:'<iframe id="_$_f_"'+n.id+' src="'+window.__base+n.attributes.openView+'?_moduleId='+n.id+'" width="100%" height="100%" frameborder="0" allowTransparency="true" scrolling="auto" onload="iframeOnload(this);"/></iframe>'
			});
		}
		//document.getElementById('endIeStatus1').click();//解决Iframe IE加载不完全的问题
		tabPanel.setActiveTab(page);
	}
}

function iframeOnload(frm) {
	//iframe高度自适应
	frm.height = (Ext.getDoc(frm)).dom.body.scrollHeight;
	//取消mask
	var tabPanel = Ext.getCmp('_$_center_panel');
  	tabPanel.getEl().unmask();
}
